package wak.up.ui;

import wake.up.AlarmList;
import wake.up.R;
import android.content.Context;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ToggleButton;
import beans.AlarmsGestionnaire;

public class AlarmAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private AlarmsGestionnaire alarmgestionnaire;
	private AlarmList alarmList;

	public AlarmAdapter(Context context, AlarmsGestionnaire alarmeGestionnaire,
			AlarmList parent) {
		inflater = LayoutInflater.from(context);
		this.alarmList = parent;

		this.alarmgestionnaire = alarmeGestionnaire;

	}

	public int getCount() {
		// TODO Auto-generated method stub
		return alarmgestionnaire.getAlarmList().size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return alarmgestionnaire.getAlarmList().get(position);
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {

			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.alarm, null);
		} else {

			holder = (ViewHolder) convertView.getTag();

		}
		holder.setPosition(position);

		holder.setAlarmeheure((TextView) convertView
				.findViewById(R.id.alarmeheure));
		holder.setIsalarmactive((ToggleButton) convertView
				.findViewById(R.id.isActiveAlarme));

		convertView.setTag(position);
		ToggleButton toggle = (ToggleButton) convertView
				.findViewById(R.id.isActiveAlarme);
		toggle.setTag(holder);
		TextView tv = (TextView) convertView.findViewById(R.id.alarmeheure);
		tv.setTag(holder);

		tv.setOnLongClickListener(new View.OnLongClickListener() {
			public boolean onLongClick(View v) {
				TextView tv2 = (TextView) v;
				ViewHolder holder = (ViewHolder) tv2.getTag();
				alarmList.deleteAlarm(holder.getPosition());
				return true;
			}
		});

		Time time = alarmgestionnaire.getAlarmList().get(position).getHeure();
		

		String texte = "";
		if (time.hour < 10) {
			texte += "0";
		}
		texte += time.hour;
		texte += ":";
		if (time.minute < 10) {
			texte += "0";
		}
		texte += time.minute;

		holder.getAlarmeheure().setText(texte);
		
		holder.getIsalarmactive().setChecked(
				alarmgestionnaire.getAlarmList().get(position).isActive());
		convertView.setTag(holder);
		return convertView;

	}
}
